// components/sku-view/index.js

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    showPop: Boolean,
    model:{
      type:Object,
    },
    statusBarHeight:Number
  },

  /**
   * 组件的初始数据
   */
  data: {
    showPop: false,
    animationData: {},
    bottomList: ["加入购物车","立即购买"],
    count:1,
    
  },

  /**
   * 组件的方法列表
   */
  methods: {

    skuChoose:function(e){
      this.properties.model.goodsSkuNames[e.currentTarget.dataset.index].index = e.currentTarget.dataset.childindex;
      this.getSkuModel()
    },

    setCount:function(e){
      var count = e.currentTarget.dataset.count
      if (!e.currentTarget.dataset.bool) {
        if (count<=1) {
          return
        }
        count -= 1
      }else{
        count += 1
      }
      this.setData({
        count: count
      })
    },


    getSkuModel(){
      var sku = null
      this.properties.model.goodsSkuNames.forEach(element => {
        if(element.index<0){
          console.log(element.index)
          return
        }
        if (sku) {
          sku += ";;" + element.index
        }else{
          sku = "" + element.index
        }
      });
      this.properties.model.goodsSkus.forEach(element => {
        if(sku == element.skuIndex){
          this.properties.model.skuModel = element
          return
        }
      });
      console.log(this.properties.model)
      this.setData({
        model:this.properties.model
      })
    },

    bottomAction:function(e){
      if (!this.properties.model.skuModel) {
        wx.showToast({
          title: '请选择规格',
          icon: 'none',
        })
        return
      }
      this.triggerEvent("stopPop", {
        index:e.currentTarget.dataset.index,
        model:this.properties.model,
        count:e.currentTarget.dataset.count
      })
      this.hideModal()
    },

    // 显示底部弹层
  showModal: function() {
    var _this = this;
    var animation = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease',
      delay: 0
    })
    _this.animation = animation
    animation.translateY(300).step()
    _this.setData({
      animationData: animation.export(),
      showPop: true
    })
    setTimeout(function() {
      animation.translateY(0).step()
      _this.setData({
        animationData: animation.export()
      })
    }.bind(_this), 50)
  },
  
  // 隐藏底部弹层
  hideModal: function() {
    var _this = this;
    // _this.triggerEvent("stopPop", 'Hello Grayly')
    // this.triggerEvent("stopPop", 'Hello Grayly')
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 500,
      timingFunction: "ease",
      delay: 0
    })
    _this.animation = animation
    animation.translateY(300).step()
    _this.setData({
      animationData: animation.export(),
    })
    setTimeout(function() {
      animation.translateY(0).step()
      _this.setData({
        animationData: animation.export(),
        showPop: false
      })
    }.bind(this), 200)
  },
  }
})
